package com.base.logistics.sf.request;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.base.core.utils.JacksonUtils;
import com.base.logistics.config.SfConfig;
import com.base.logistics.sf.bean.RequestParams;
import com.base.logistics.sf.bean.order.*;
import com.base.logistics.sf.bean.print.PrintRespResponse;
import com.base.logistics.sf.bean.print.PrintSfOrderDocument;
import com.base.logistics.sf.bean.print.PrintSfOrderRequest;
import com.base.logistics.sf.bean.print.PrintWaybillRequest;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 * 顺丰订单api接口实现
 * </pre>
 * @since 2021-02-14
 */
@Component
public class SfPrintApiRequest {

    private SfRequestConfig sfApiRequestConfig;

    @Autowired
    public SfPrintApiRequest(SfRequestConfig sfApiRequestConfig) {
        this.sfApiRequestConfig = sfApiRequestConfig;
    }


    /**
     * 获取打印参数数据
     * @param request
     * @return
     * @throws Exception
     */
    public PrintSfOrderRequest requestPrintParam(OrderRespRequest request, PrintWaybillRequest waybillRequest) throws Exception {
        SfConfig config = this.sfApiRequestConfig.getConfig();
        RequestParams params = new RequestParams(request.getServiceCode(),request.toJson());
        OrderRespResponse response = OrderRespResponse.fromJson(this.sfApiRequestConfig.post(params));
        System.out.println("订单下单结果:"+ JacksonUtils.toJSONString(response));
        String w = null;
        String b = null;
        List<String> s = new ArrayList<>();
        List<WaybillNoInfo> waybillNoInfoList = response.getWaybillNoInfoList();
        for (WaybillNoInfo info : waybillNoInfoList) {
            if(1 == info.getWaybillType()){
                w = info.getWaybillNo();
            }else if(2 == info.getWaybillType()){
                s.add(info.getWaybillNo());
            }else {
                b = info.getWaybillNo();
            }
        }
        int seq = 1;
        List<RouteLabelInfo> routeLabelInfo = response.getRouteLabelInfo();
        List<PrintSfOrderDocument> documents = new ArrayList<>();
        for (RouteLabelInfo labelInfo : routeLabelInfo) {
            if(!"1000".equals(labelInfo.getCode())){
                throw new Exception(labelInfo.getMessage());
            }
            RouteLabelData data = labelInfo.getRouteLabelData();
            PrintSfOrderDocument document = new PrintSfOrderDocument();
            if(StrUtil.isNotBlank(b)){
                document.setBackWaybillNo(b);
            }else{
                document.setMasterWaybillNo(w);
                if(s.contains(data.getWaybillNo())){
                    document.setBranchWaybillNo(data.getWaybillNo());
                }
            }
            document.setSeq(String.valueOf(seq));
            document.setSum(String.valueOf(waybillNoInfoList.size()));
            document.setIsPrintLogo(config.getPrintLogo());
            document.setSystemSource("scp");
            document.setPrintNum("10");
            document.setPrintDateTime(DateUtil.now());
            document.setAgingType(data.getProCode());
            document.setAgingText(data.getProCode());
            document.setDestRouteLabel(data.getDestRouteLabel());
            document.setDestTeamCode(data.getDestTeamCode());
            document.setCodingMapping(data.getCodingMapping());
            document.setTwoDimensionCode(data.getTwoDimensionCode());
            document.setAbFlag(data.getAbFlag());
            document.setCodingMappingOut(data.getCodingMappingOut());
            document.setProductName(data.getProName());
            document.setSourceTransferCode(data.getSourceTransferCode());
            document.setPrintIcons(new ArrayList<>());
            BeanUtils.copyProperties(document,waybillRequest);
            documents.add(document);
            seq ++;
        }
        PrintSfOrderRequest r = new PrintSfOrderRequest(this.sfApiRequestConfig.getConfig());
        r.setDocuments(documents);
        r.setCommond("print");
        return r;
    }

    public Boolean printData(OrderRespRequest request, PrintWaybillRequest waybillRequest) throws Exception {
        PrintSfOrderRequest requestParam = this.requestPrintParam(request,waybillRequest);
        RequestParams r = new RequestParams(requestParam.getServiceCode(),requestParam.toJson());
        r.put("fileType",requestParam.getFileType());
        r.put("templateCode",requestParam.getTemplateCode());
        r.put("documents", requestParam.getDocuments());
        System.out.println("[printParam]:"+JacksonUtils.toJSONString(r));
        PrintRespResponse printRespResponse = PrintRespResponse.fromJson(this.sfApiRequestConfig.post(r));
        return printRespResponse.isSuccess();
    }


}
